/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 *
 * @author Mau
 */
public class main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
        
        File filein = null, fileout = null;
        FileReader stream;
        int numChRead;
        if ( args.length != 2 ) {
            System.out.println("Use only two parameters, the file to be compiled and the output");
        }
        else {
            filein = new File(args[0]);
            fileout = new File(args[1]);
        }
        if ( !filein.exists() || !filein.canRead() ) {
            System.out.println("Either the input file " + args[0] + " does not exist or it cannot be read");
            throw new RuntimeException();
        }
        
        try {
            stream = new FileReader(filein);
        } catch ( FileNotFoundException e ) {
            System.out.println("Something wrong: input file does not exist anymore");
            throw new RuntimeException();
        }
       
        // one more character for '\0' at the end that will be added by the
        // compiler
        char []input = new char[ (int ) filein.length() + 1 ];
        try {
            numChRead = stream.read( input, 0, (int ) filein.length() );
        } catch ( IOException e ) {
            System.out.println("Error reading file " + args[0]);
            throw new RuntimeException();
        }
//        if ( numChRead != filein.length() ) {
//           System.out.println("Read error");
//            throw new RuntimeException();
//        }
        try {
            stream.close();
        } catch ( IOException e ) {
            System.out.println("Error in handling the file " + args[0]);
            throw new RuntimeException();
        }
        
        //char []input = "inteiro 3 \"mauricio\" \n 4.156 4.14 3 {_TITE} 3.5 ".toCharArray();
        Compiler compiler = new Compiler();
        compiler.compile(input, args[1]);        
    }
}
